package persistencia.csv;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;

public class EscritorCSV extends Writer {

	protected BufferedWriter salida;
	protected Character separador;
	boolean fin;
	
	public EscritorCSV (String archivo, Character separador) throws IOException {
		FileWriter fw = new FileWriter(archivo);
		
		salida = new BufferedWriter(fw);
		this.separador = separador;
	}
	
	@Override
	public void write(char[] cbuf, int off, int len) throws IOException {
		salida.write(cbuf, off, len);
	}

	public void escribir(String[] valores) throws IOException {
		String linea = null;
		if (valores.length > 0)
			linea = valores[0];
		
		for (int i=1; i<valores.length; i++)
			linea+= separador + valores[i];
		
		if (linea != null)
			salida.write(linea+"\n");
	}
	
	public void escribir(Iterator<String> valores) throws IOException {
		String linea = null;
		
		if (valores.hasNext()) {
			linea = valores.next();
			
			while (valores.hasNext())
				linea+= separador + valores.next();
			
			salida.write(linea+"\n");
		}
	}
	

	@Override
	public void close() throws IOException {
		salida.close();
	}

	@Override
	public void flush() throws IOException {
		salida.flush();
	}		
}
